<template>
  <div class="m-view-purchasesApplyList m-window">
    <div class="m-condition">
      <el-form :model="searchForm">
        <el-row :gutter="10">
          <el-col :span="6">
            <el-form-item label="工厂代码：" label-width="88px">
              <el-input
                v-model="searchForm.factory"
                placeholder="输入工厂代码"
                clearable
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="物料编号：" label-width="88px">
              <el-input
                v-model="searchForm.matNum"
                placeholder="输入物料编号"
                clearable
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="物料描述（材质）：" label-width="140px">
              <el-input
                v-model="searchForm.matDescription"
                placeholder="输入物料描述"
                clearable
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="交货日期：" label-width="88px">
              <el-date-picker
                v-model="searchForm.deliveryDate"
                type="date"
                placeholder="输入日期"
                value-format="yyyy-MM-dd"
                :editable="false"
              >
              </el-date-picker>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="10">
          <el-col :span="6">
            <el-form-item label="采购申请：" label-width="88px">
              <el-input
                v-model="searchForm.purchaseRequestSap"
                clearable
              ></el-input>
            </el-form-item>
          </el-col>

          <el-col :span="3" class="m-floatright">
            <el-button type="primary" @click="handleCreateSapRequest">创建SAP采购申请</el-button>
          </el-col>

          <el-col :span="2" class="m-floatright">
            <el-button type="primary" @click="getPurchaseRequest">查询</el-button>
          </el-col>
        </el-row>
      </el-form>
    </div>
    <div class="m-agTable m-bottom m-applySoleListTable">
      <ag-grid-vue
        class="m-agTableContent ag-theme-balham m-agCenter"
        :gridOptions="gridOptions"
        :columnDefs="columnDefs"
        :rowData="rowData"
       
        v-loading="loading"
      ></ag-grid-vue>
    </div>
      <div class="m-page">
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        @prev-click="handlePrevClick"
        @next-click="handleNextClick"
        :current-page="searchForm.current"
        :page-sizes="[30, 50, 100, 150, 200]"
        :page-size="searchForm.size"
        layout="total, sizes, prev, pager, next, jumper"
        :total="searchForm.total"
      ></el-pagination>
    </div>
  </div>
</template>
<script>
import gridOptions from "lesso-common/public/gridOptions";
import _Api from "@/api";
import constant from "@/public/constant";

export default {
  data() {
    return {
      searchForm: {
        factory: "", //工厂代码
        matNum: "", //物料编号
        matDescription: "", //材质
        deliveryDate: "", //交货日期
        purchaseRequestSap: "", //采购申请
        size:30,
        current:1,
         total:0,
      },
      //ag-grid 配置项
      gridOptions: null,
      columnDefs: null,

      rowData: [], //  表格数据
      loading: false, //加载条
    };
  },
  beforeMount() {
    
  },
  created(){
this.gridOptions = gridOptions();
    this.columnDefs = [
       {
        headerName: "申购单号",
        field: "purchaseNum",
        checkboxSelection: true,
        headerCheckboxSelection: true,
        headerCheckboxSelectionFilteredOnly: true,
         width: 160 
      },
      {
        headerName: "采购申请",
        field: "purchaseRequestSap",
      width: 95,
      },
      { headerName: "行号", field: "sequence", width: 68 },
      { headerName: "采购申请类型", field: "purchaseRequestType", width: 115 },
      { headerName: "科目分配类别", field: "accountAssignmentCategory", width: 115 },
      { headerName: "项目类别", field: "projectType" , width: 90}, //---
      { headerName: "物料编号", field: "matNum", width: 95 },
      { headerName: "材质", field: "matDescription", width: 68 },
      { headerName: "申请数量", field: "amount", width: 90 }, 
      { headerName: "单位", field: "unit", width: 68 },
      { headerName: "评估价格", field: "appraisalPrice", width: 90 },
      { headerName: "价格单位", field: "priceUnit" , width: 90 },
      { headerName: "交货日期", field: "deliveryDate", width: 95  },
      { headerName: "物料组", field: "materialGroup" , width: 95 },

      { headerName: "工厂", field: "factory", width: 68  },
      { headerName: "库存地点", field: "stockLocation" , width: 90 },
      { headerName: "采购组", field: "purchaseGroup", width: 80  },
      { headerName: "申请者", field: "applicant" , width: 80 },
      { headerName: "成本中心", field: "costCentre", width: 90  },
      { headerName: "项目文本", field: "itemText" , width: 95 },
      { headerName: "供货工厂", field: "supplyFactory", width: 90  },
      { headerName: "采购组织", field: "purchaseOrg", width: 90  }, //---
      { headerName: "批次", field: "batch", width: 95  },
      { headerName: "需求跟踪号", field: "requirementTrackNum", width: 110  },
    ];
     this.gridOptions.defaultColDef.autoHeight = true;
    this.gridOptions.defaultColDef.cellClass = "cell-wrap-text";
  },
  mounted(){
   this.searchForm.deliveryDate = this.$utils.dateFormat('yyyy-MM-dd', new Date());   //设置默认日期今日
  this.getPurchaseRequest();
 },
  methods:{
  
  // 查询列表
    getPurchaseRequest() {
      
      let params = {
        size: this.searchForm.size,
        current: this.searchForm.current,
         leParams:{
          state:5
        },
        geParams:{
           state:4
        },
        eqParams: {
          state: this.searchForm.state,
        }, //精确查询
        likeParams: {
          factory: this.searchForm.factory,
          matNum: this.searchForm.matNum,
          matDescription: this.searchForm.matDescription,
          deliveryDate: this.searchForm.deliveryDate,
          purchaseRequestSap: this.searchForm.purchaseRequestSap,

        }, //模糊查询
        orderBy: "createDate",
        isAsc: "false",
      };
      this.loading = true;
      _Api
        .getPurchaseRequestList(params)
        .then((response) => {
          let data = response.data;
            this.searchForm.total = data.total;
          if (data.code == 1 && data.total != 0) {
            this.rowData = data.rows;
            this.loading = false;
          } else {
            this.loading = false;
            this.rowData = [];
          }
        })
        .catch((err) => {
          this.rowData = [];
          this.loading = false;
          this.$alert(err.data.message, {
            showClose: false,
            confirmButton: "确定",
            customClass: "alertFailure",
          });
        });
    },
      // 每页显示条数改变回调
    handleSizeChange(val) {
      this.searchForm.size = val;
      this.getPurchaseRequest();
    },
    // 页码改变回调
    handleCurrentChange(val) {
      this.searchForm.current = val;
      this.getPurchaseRequest();
    },
    // 点击上一页回调
    handlePrevClick(val) {
      this.searchForm.current = val;
    },
    // 点击下一页回调
    handleNextClick(val) {
      this.searchForm.current = val;
    },
    // 创建sap采购申请
    handleCreateSapRequest(){
       let rows = this.gridOptions.api.getSelectedRows();
      
         if (rows.length <= 0) {
        this.$alert("提示：请选择需要创建SAP的数据", {
          showClose: false,
          confirmButtonText: "确定",
          customClass: "alertPrompt",
        });
        return;
      };
       let updateRows = [];
      rows.forEach((item) => {
        let obj = {};
        obj = item;

         obj.modifyer=constant.user.loginName;//登录人名称
        obj.modifyerId=constant.user.id;//登录人Id
        // obj.state = 5;
        updateRows.push(obj);
      });
       _Api
        .createSapRequest(updateRows)
        .then((response) => {
          let data = response.data;
       
          if (data.code == 1) {
            this.$message({
              message: data.msg,
              type: "success",
            });
            this.getPurchaseRequest();
          } else {
            this.$message({
              message: data.msg,
              type: "warning",
            });
          }
        })
        .catch((err) => {
          this.$alert(err.data.message, {
            showClose: false,
            confirmButton: "确定",
            customClass: "alertFailure",
          });
        });
    }
  }
};
</script>
<style lang="less">
.cell-wrap-text {
  white-space: normal !important;
}
</style>